package 每日一题;

public class 第N个数 {
    //题解：https://leetcode-cn.com/problems/nth-digit/solution/pythonjavajavascriptgo-jian-dan-mo-ni-by-kk3x/
    public int findNthDigit(int n) {
        // 1. 找该数字属于哪个范围
        // 2. 找到这个数字
        // 3. 找到是这个数字的第几位数字
        int len = 1, base = 9;
        while (n > (long) len * base) {
            n -= len * base;
            len++;
            base *= 10;
        }
        // n = 11
        //len = 3 base=900
        int start = base / 9; //start = 100
        int num = start + (n - 1) / len; // num = 103
        int dgtIdx = (n - 1) % len;  // 1
        return String.valueOf(num).charAt(dgtIdx) - '0';
    }
}
